#include "CMapReader.h"


CMapReader::CMapReader()
{
	m_pRaw			= NULL;
	m_nMapWidth		= 0;
	m_nMapHeight	= 0;
}


CMapReader::~CMapReader()
{
	Release();	
}


//------------------------------------------------//
// Name : Release								  //
// Desc : Release the pointer , free memory etc.. //
//------------------------------------------------//
void CMapReader::Release()
{
	if(m_pRaw)
		m_pRaw->Release();

	m_pRaw = NULL;
}



//---------------------------------------------//
// Name : LoadRawMap						   //
// Desc : Load a png file to read as a raw map //
//---------------------------------------------//
HRESULT CMapReader::LoadRawMap(LPCTSTR FileName , LPDIRECT3DDEVICE9 pD3dDevice )
{
	HRESULT					hr;
	D3DXIMAGE_INFO			imageInfo;

	if(!pD3dDevice)
		return D3DERR_INVALIDCALL;

	if( m_pRaw )
		return D3DERR_INVALIDCALL;

	//Get the raw map info
	hr = D3DXGetImageInfoFromFile( FileName , &imageInfo );

	if(FAILED(hr))
		return hr;

	//Create a raw map as a texture file with a 32-bit format
	hr = D3DXCreateTextureFromFileEx( pD3dDevice ,							// DirectX device
										FileName ,							// File Path
										imageInfo.Width ,					// width
										imageInfo.Height ,					// Height
										imageInfo.MipLevels ,				// MipLevels
										D3DUSAGE_DYNAMIC ,					// D3DUSAGE
										D3DFMT_A8R8G8B8 ,					// Format
										D3DPOOL_DEFAULT ,					// Pool
										D3DX_DEFAULT ,						// Filter
										D3DX_DEFAULT ,						// Mip filter
										0 ,									// Color key
										&imageInfo ,						// Image Info
										NULL ,								// NULL
										&m_pRaw );							// Output texture

	//Store the raw map width and height
	m_nMapWidth		= imageInfo.Width;
	m_nMapHeight	= imageInfo.Height;


	if(FAILED(hr))
		return hr;

	return S_OK;
}




//-----------------------------------------------//
// Name : GetRawMap								 //
// Desc : Get the raw map as a D3DCOLOR 2D array //
//-----------------------------------------------//
D3DCOLOR** CMapReader::GetRawMap()
{
	HRESULT hr;
	D3DLOCKED_RECT rect;
	D3DSURFACE_DESC desc;
	DWORD i,j;
	D3DCOLOR** pColor;

	//First get the D3DSURFACE_DESC of this raw map
	hr = m_pRaw->GetLevelDesc( 0 , &desc );
	if(FAILED(hr))
		return 0;

	//We create a new D3DCOLOR 2D array has enough room to hold pixel color 
	pColor = new D3DCOLOR*[desc.Height];
	for ( i = 0 ; i < desc.Height ; ++i)
		pColor[i] = new D3DCOLOR[desc.Width];

	//Lock entire texture to read pixel
	hr = m_pRaw->LockRect( 0 , &rect , NULL , D3DLOCK_READONLY );
	if(FAILED(hr))
		return 0;

	BYTE* pBits = (BYTE*)rect.pBits;

	for (i = 0 ; i < desc.Height ; ++i)
	{
		for (DWORD j = 0 ; j < desc.Width ; ++j)
		{
			DWORD index =( j*4 + ( i * (rect.Pitch) ));
       
			// Blue
			BYTE b = pBits[index];

			// Green
			BYTE g = pBits[index+1];

			// Red
			BYTE r = pBits[index+2];

			// Alpha
			BYTE a = pBits[index+3];
			D3DCOLOR color = D3DCOLOR_ARGB(a , r , g , b );
			pColor[i][j] = color;
		}
	}

	//Unlock texture
	m_pRaw->UnlockRect(0);

	//Return a D3DCOLOR 2D array
	return pColor;
}



//---------------------------------------------//
// Name : GetMapWidth						   //
// Desc : Get the width of the current raw map //
//---------------------------------------------//
int CMapReader::GetMapWidth()
{
	return m_nMapWidth;
}




//----------------------------------------------//
// Name : GetMapHeight						    //
// Desc : Get the height of the current raw map //
//----------------------------------------------//
int CMapReader::GetMapHeight()
{
	return m_nMapHeight;
}